#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
	qDebug() << "Info : start Mainwindow...";
	setupUi(this);//Init de la framme
	GB_Work->setVisible(false);
//	Otis = new xmlScribe;
        Edit_FolderOut->setText(QCoreApplication::applicationDirPath() + "/Upload/update.lst");
	if(CBoxAfficherMsgBox->checkState() == 0)
        {treeWidget->setFloodMsgBox(false);}
	else if (CBoxAfficherMsgBox->checkState() == 2)
        {treeWidget->setFloodMsgBox(true);}
	GB_edit->setVisible(false);
	connectland();
	Set_Status_Msg("Welcome to the APDVM builder");
    treeWidget->setFilelevel(checkBox->checkState());
	adjustSize();
}
MainWindow::~MainWindow()
{
//	delete Otis;
	qDebug() << "Info :  stop Mainwindow...";
}
void MainWindow::connectland()
{
	connect(Btn_Close, SIGNAL(clicked()), qApp, SLOT(closeAllWindows()));
	connect(Btn_First_Ignore, SIGNAL(clicked()),this, SLOT(START_IgnorePath()));
	connect(Btn_First_Ok, SIGNAL(clicked()),this, SLOT(START_SetUpdaterPath()));
	connect(Btn_First_Search, SIGNAL(clicked()),this, SLOT(START_SearchPathUpdater()));
	connect(Btn_Item_addfolder, SIGNAL(clicked()),treeWidget, SLOT(AddFolder()));
	connect(Btn_Item_addfile, SIGNAL(clicked()),treeWidget, SLOT(AddFile()));
	connect(Btn_Item_remove, SIGNAL(clicked()),treeWidget, SLOT(DeleteSelectedFile()));

    connect(Btn_Apply_Edit, SIGNAL(clicked()), this, SLOT(Liste_Edit_Apply()));
    connect(checkBox, SIGNAL(clicked(bool)),treeWidget, SLOT(setFilelevel(bool)));

    connect(CBoxAfficherMsgBox, SIGNAL(stateChanged(int)), this, SLOT(EnableMsgBox(int)));
	connect(treeWidget,SIGNAL(TaskToShowInStatusbar(QString)), this, SLOT(Set_Status_Msg(QString)));
	connect(treeWidget,SIGNAL(TaskToShowGbEdit(bool)), this, SLOT(Liste_Show_Edit(bool)));
	connect(treeWidget,SIGNAL(TaskToEnableGUI(bool)), this, SLOT(EnableControls(bool)));
	connect(Btn_Apply, SIGNAL(clicked()), this, SLOT(LaunchGen()));
        connect(&OXM_Generator,SIGNAL(NotifyErrorMsg(QString)),this,SLOT(ShowMsgError(QString)));
        connect(&OXM_Generator,SIGNAL(NotifyMsg(QString)),this,SLOT(ShowMsg(QString)));
        connect(&OXM_Generator,SIGNAL(NotifyProgress(int, int)),this,SLOT(Showprogress(int, int)));
        connect(treeWidget,SIGNAL(NotifyProgress(int, int)),this,SLOT(Showprogress(int, int)));
        connect(&OXM_Generator,SIGNAL(finished()),this,SLOT(BuildFinished()));
}
void MainWindow::ShowMsgError(QString VCP_Msg)
{
        this->Set_Status_Msg(VCP_Msg);
    //TODO : msgBox
}
void MainWindow::ShowMsg(QString VCP_Msg)
{
        this->Set_Status_Msg(VCP_Msg);
	//TODO : msgBox
}
void MainWindow::Showprogress(int VLP_Value, int VLP_ValueMax)
{
        Prgbar_Main->setValue(VLP_Value);
        Prgbar_Main->setMaximum(VLP_ValueMax);
}
void MainWindow::START_SearchPathUpdater()
{
	QString fileName = QFileDialog::getOpenFileName(
                         this,
                         trUtf8("Selectionnez l'updater"));

	Line_First_Select->setText(fileName);
	if (!fileName.isEmpty())
	{
		Btn_First_Ok->setEnabled(true);
	}
}
void MainWindow::START_SetUpdaterPath()
{
	FirstWidget->setVisible(false);
	GB_Work->setVisible(true);
	adjustSize();
	QFileInfo FI_updater(Line_First_Select->text());
        VCM_PathUpdater = FI_updater.path();
        treeWidget->Set_Path_Updater(VCM_PathUpdater);
        Set_Status_Msg("Updater choisi dans le dossier : " + VCM_PathUpdater);
        treeWidget->Insert_updater(VCM_PathUpdater, FI_updater.fileName(), 100);
}
void MainWindow::START_IgnorePath()
{
	FirstWidget->setVisible(false);
	GB_Work->setVisible(true);
	adjustSize();
}
void MainWindow::EnableMsgBox(int VLP_NewStatus)
{
        if(VLP_NewStatus == 0)
        {treeWidget->setFloodMsgBox(false);}
        else if (VLP_NewStatus == 2)
        {treeWidget->setFloodMsgBox(true);}
}
void MainWindow::Set_Status_Msg(QString VCP_NewText, int VLP_TimeShowed)
{
        statusBar()->showMessage(VCP_NewText, VLP_TimeShowed);
	
}
void MainWindow::Liste_Show_Edit(bool VBP_NewState)
{
        GB_edit->setVisible(VBP_NewState);
    checkBox_edit->setCheckable(true);
}
void MainWindow::Liste_Edit_Apply()
{
    bool check;
    char priority;
    check=checkBox_edit->checkState();
    if( check == true)
        priority='c';
    else
    {
        priority='n';
    }
    treeWidget->SetLevelSelectedFiles(priority);
}
void MainWindow::EnableControls(bool VBP_SetValue)
{
        Btn_Item_addfolder->setEnabled(VBP_SetValue);
        //Btn_Item_addfile->setEnabled(VBP_SetValue);
        checkBox->setEnabled(VBP_SetValue);
        Btn_Item_remove->setEnabled(VBP_SetValue);
        GB_edit->setEnabled(VBP_SetValue);
        tabWidget->setEnabled(VBP_SetValue);
        Btn_Apply->setEnabled(VBP_SetValue);
}
void MainWindow::LaunchGen()
{
	//On envoie la liste
        OXM_Generator.setLists(treeWidget->getList());
        //On envoie le fichier de sortie :
        OXM_Generator.setOutPutFilePath(Edit_FolderOut->text());
        OXM_Generator.setReferenceFilePath(QCoreApplication::applicationDirPath() + "/Upload/");
	//On désactive l'IHM
	EnableControls(FALSE);
	//On lance le Build
        OXM_Generator.start();
	
}
void MainWindow::BuildFinished()
{
        if(OXM_Generator.getLatestError() == BUILDER_NO_ERROR)
        {
                this->ShowMsg("Build Finished");
        }
        //On réactive l'IHM
        EnableControls(TRUE);
}

#ifdef CODE_SUPPRIME_A_LA_COMPILATION_NON_LISTER_DANS_CDC_V1_0_0
void MainWindow::BrowseXML()
{
        QString fileName = QFileDialog::getOpenFileName(
                         this,
                         trUtf8("Selectionnez le fichier a lire"));
        if (fileName.isEmpty())
        {return;}
        else
        {Edit_OldUpdatesLst->setText(fileName);}
}

void MainWindow::ReadXML()
{
        QFileInfo XML_list(Edit_OldUpdatesLst->text());
        Edit_FolderOut->setText(XML_list.absoluteFilePath());
        PATH_XMLList = XML_list.path();
        OXM_Generator.setPathXML(PATH_XMLList);
//	Otis->ReadFile(Edit_OldUpdatesLst->text());
        Btn_Apply->setEnabled(true);
}

void MainWindow::EnableCheckDoublon(int newstatus)
{
        if(newstatus == 0)
        {treeWidget->setCheckDoublon(false);}
        else if (newstatus == 2)
        {treeWidget->setCheckDoublon(true);}
}
#endif
